package com.woniuxy.demo.config;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.ai.vectorstore.redis.autoconfigure.RedisVectorStoreProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPooled;
@Configuration
public class RedisStackConfig {
    /**
     * 配置Redis连接池
     * 从配置文件读取主机和端口信息
     */
    @Bean
    public JedisPooled jedisPooled(RedisConnectionDetails redisConnectionDetails) {
        // 从RedisConnectionDetails中获取配置的主机和端口，创建Jedis连接池
        // （连接信息来自application.properties中的spring.redis配置）
        return new JedisPooled(
                redisConnectionDetails.getStandalone().getHost(),
                redisConnectionDetails.getStandalone().getPort());
    }

    /**
     * 配置Redis向量存储
     * 使用自定义前缀，确保数据隔离
     */
    @Bean
    public VectorStore myRedisVectorStore(
            JedisPooled jedisPooled,
            EmbeddingModel embeddingModel,
            RedisVectorStoreProperties properties) {
        // 创建RedisVectorStore实例
        return RedisVectorStore.builder(jedisPooled, embeddingModel)
                .prefix(properties.getPrefix())                  // Optional: 设置键前缀（从配置文件中读取）
                .initializeSchema(true)                   // Optional: 自动初始化Redisearch索引（首次使用时创建）
                .batchingStrategy(new TokenCountBatchingStrategy())// Optional: 设置文档分块策略（按令牌数）
                .build();
    }
}
